<?php
include '../config/auth.php';
$username=$_SESSION['user'];
$config = include '../config/db.php';
$db = new PDO($config['dsn'],$config['username'],$config['password']);
$statement=$db->prepare('delete from user_room where user_name=:user_name');
$result=$statement->execute([
    ':user_name'=>$username,
]);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>星星聊天系统</title>
    <link type="text/css" href="../css/Index.css" rel="stylesheet">
</head>
<body>
<div id="all">
    <div class="top">
        <div class="left">
            <?php
            include '../config/welcome.php';
            ?>
        </div>
        <div class="right">
            <form id="myForm" action="../action/IndexAction.php" method="post">
                <input style="width: 80px" type="text"  name="text">
                <button style="width: 80px" type="button" id="submit">查找房间</button>&nbsp;&nbsp;
                <a href="MakeRoom.php">新建聊天室</a>&nbsp;&nbsp;
                <a href="Update.php">修改资料</a>&nbsp;&nbsp;&nbsp;
                <label id="out">退出</label>&nbsp;&nbsp;&nbsp;
            </form>
        </div>
    </div>
    <script>
        var outElement=document.getElementById('out');
        outElement.onclick=function(){
            window.location.href="../config/out.php";
        }
        var submitElement=document.getElementById('submit');
        var formElement=document.getElementById('myForm');
        submitElement.onclick = function (e) {
            e.preventDefault();
            var textElement =document.querySelector('#myForm input[name=text]');
            var content= textElement.value;
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {   //等待远程主机做出回应
                if (this.readyState == 4) {
                    if (this.status == 200) {
                        var result=JSON.parse(this.responseText);
                        if(result.error==1){
                            alert(result.message);
                        }else {
                            window.location.href="ChatRoom.php?room_id="+content;
                        }
                    } else {
                        alert('服务器出现异常，请及时联系管理员')
                    }
                }
            };
            xhr.open( formElement.method,formElement.action);
            var data=new FormData();
            data.append('room_id',content);
            xhr.send(data);
        };
    </script>
    <div class="center">
        <?php
        $statement=$db->query('select * from make_room');
        $result=$statement->fetchAll();
        foreach($result as $value) {
            echo '<div class="tip ">';
            echo '<div class="check">';
            echo "<a href='ChatRoom.php?room_id=".$value['room_id']."'><img src=".$value['room_path']."></a>";
            echo "</div>";
            echo '<div class="bottom">';
            echo "房间号：".$value['room_id']."<br>";
            echo "房间类型：".$value['room_type'];
            echo "</div>";
            echo "</div>";
        }
        ?>
    </div>
</body>
</html>
